"""
a really poor popup box.

smart enough to display text and perhaps images
in a grid like or column like fashion.

   image  |  text about image
          |  can span multiple
          |  lines.

clicking a mouse button will exit."""

import pygame
from pygame.locals import *

import data
import constants as c

def game_completed(display):
    'show the user a nice ending screen. They deserve it.'
    img, rect = data.load_image('gamecomplete.png', None)
    display.blit(img, (0, 0))
    pygame.display.flip()

    wait_for_interaction()
    
def success_or_fail(display, cond, stats):
    'show the success or failure screen'
    img, rect = data.load_image('success.png' if cond else 'failure.png', None)
    display.blit(img, (0, 0))
    pygame.display.flip()

    heading = pygame.font.Font(None, 36)
    normaltext = pygame.font.Font(None, 30)

    # show the user his stats.
    txt = ('Target Energy: %s kWh\nProduced Energy: %s kWh'
            % (stats.target_kWh, stats.total_kWh))
    y = rendertxt(display, txt, normaltext, 500, 100)
    for bonus in stats.bonus:
        # bonus is a surface already to be shown.
        display.blit(bonus, (500 - 60, y))
        y += bonus.get_height()

    pygame.display.update()


    wait_for_interaction()

def popup(display, config):
    'a full screen instructions before play popup'

    original = display.copy()
    # get a rectangle that is smaller than the original background.
    rect = display.get_rect().inflate(-20, -20)

    # make a new surface to draw this popup to.
    pop_surf = pygame.Surface((rect.width, rect.height))

    pop_surf.fill((100, 100, 100))

    heading = pygame.font.Font(None, 36)
    normaltext = pygame.font.Font(None, 24)
    rendertxt(pop_surf, config.name, heading, 80, 50)
    y = 100

    y = rendertxt(pop_surf, 'Goal', heading, 100, y)

    text = ('To pass the level you must produce %s kWh or more' %
            config.target_kWh)
    y = rendertxt(pop_surf, text, normaltext, 120, y)

    text = ('* A turbine hit by a bird will not produce energy'
            ' for a period of time proportional to the momentum'
            ' of the bird')

    y = rendertxt(pop_surf, 'Instructions', heading, 100, y)
    y = rendertxt(pop_surf, text, normaltext, 120, y)
    y = rendertxt(pop_surf, 'momentum = mass * speed', normaltext, 120, y)

    text = ('* Each use of the fan will consume %s kWh of your produced'
            ' energy.' % c.AIRS_COST)
    y = rendertxt(pop_surf, text, normaltext, 120, y)


    # now render the game info.
    rendertxt(pop_surf, 'Other Info', heading, 500, 100)
    timelimit = 'time limit %s seconds' % config.timelimit
    rendertxt(pop_surf, timelimit, normaltext, 550, 160)


    # get all the birds so we can blit them.
    all = pygame.sprite.Group()
    birdclasses = [(v[0], k) for k, v in config.birds.items()]

    y = 210
    for Bird, name in birdclasses:
        b = Bird(500, y, all)
        b.velx, b.vely = (0, 0)
        all.add(b)
        rendertxt(pop_surf, '%s: %s kg' % (name, b.m), normaltext, 550, y)
        y += 50

    # now show the time attributes.
    timesprite = pygame.sprite.Sprite()
    timesprite.image, timesprite.rect = data.load_image('time.png', -1)
    timesprite.rect.topleft = (500, 150)
    all.add(timesprite)


    display.blit(pop_surf, (10, 10))
    pygame.display.flip()
    time_last = pygame.time.get_ticks()


    clk = pygame.time.Clock()
    running = True
    while running:
        clk.tick(c.FPS)

        time_current = pygame.time.get_ticks()
        time_diff = time_current - time_last
        time_last = time_current

        all.update({'time': time_diff})
        display.blit(pop_surf, (10, 10))
        all.draw(display)
        pygame.display.flip()
        for e in pygame.event.get():
            if e.type == MOUSEBUTTONDOWN:
                running = False
            if e.type == QUIT:
                raise SystemExit("Poweroff")

def rendertxt(display, text, font, x, y):
    BIG_BUFFER = 30
    SMALL_BUFFER = 5
    lines = data.wrap_multi_line(text, font, 300)
    for line in lines:
        surf = font.render(line, True, c.WHITE)
        display.blit(surf, (x, y))
        y += SMALL_BUFFER + surf.get_height()
    return y + BIG_BUFFER

def wait_for_interaction():
    clk = pygame.time.Clock()
    running = True
    pygame.time.delay(500)
    pygame.event.clear()
    while running:
        clk.tick(c.FPS)

        for e in pygame.event.get():
            if e.type == MOUSEBUTTONDOWN:
                running = False
            if e.type == QUIT:
                raise SystemExit("Poweroff")


